var $;
layui.use(['form','layer','jquery','laypage'],function() {
    var form = layui.form(),
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        $ = layui.jquery;


    $(".search_btn").click(function(){
        var querystr = $(".search_input").val();
        var send_status = $('.select-status').val();
        location.href = '/goods/index?querystr=' + querystr+'&status='+send_status;
        layer.msg('查询中，请稍候');
    });

    $('.add-btn').click(function () {
        var index = layui.layer.open({
            title : "添加商品",
            type : 2,
            content : "/goods/create",
            success : function(layero, index){
                setTimeout(function(){
                    layui.layer.tips('点击此处返回权限列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        //改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
        $(window).resize(function(){
            layui.layer.full(index);
        })
        layui.layer.full(index);
    });

    $('.edit-btn').click(function () {
        var goods_id = $(this).attr('data-id');
        var index = layui.layer.open({
            title : "添加商品",
            type : 2,
            content : "/goods/edit?goods_id=" + goods_id,
            success : function(layero, index){
                setTimeout(function(){
                    layui.layer.tips('点击此处返回权限列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        //改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
        $(window).resize(function(){
            layui.layer.full(index);
        })
        layui.layer.full(index);
    });

    form.on('switch(Is_Shelves)',function(data){
       var id = $(this).attr('data-id');
       var status = $(this).attr('data-status');
        var that = $(this);
        var BackOrig= '';
        if(orig==true){
            BackOrig = false;
        }else{
            BackOrig = true;
        }

        layer.confirm('您是否确定？', {
                btn: ['确认','取消'] //按钮
            },function(){
            var index = layer.msg('修改中，请稍候',{icon: 16,time:false,shade:0.8});
                    $.ajax({
                        url:'/goods/changestatus',
                        type:'GET',
                        data:{id:id,status:status},
                        dataType:"json",
                        timeout:2000,
                        complete: function(){
                            layer.close(index);
                        },
                        success:function(ret){
                                if(ret.status==1){
                                    layer.msg(ret.msg);
                                }else{
                                    history.go(0);
                                    layer.alert(ret.msg, {icon: 2}, function(index){
                                    layer.close(index);
                                    });
                                }
                        },
                        error:function(){
                            // that.prop('checked',BackOrig);
                            history.go(0);
                            layer.alert('参数错误', {icon: 2}, function(index){
                                layer.close(index);
                            });
                        }
                    })
            });
        },function(){
            layer.msg('你已取消该操作',{
                time:2000,
            })
        });

    $(".look-detail").on('click',function(){
        var id = $(this).attr('data-id');
        var index = layui.layer.open({
            title : "查看商品详情",
            type : 2,
            content : "/goods/details?id="+id,
            success : function(layero, index){
                setTimeout(function(){
                    layui.layer.tips('点击此处返回商品列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        });
        //  改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
        $(window).resize(function(){
            layui.layer.full(index);
        });
        layui.layer.full(index);
    });

});
    $('.goods-photo-face').on('click',function(){
        layer.photos({
            photos: '.goods-photo-face'
            ,shift: 5
        });
    });
$("#goods-photos").on('click',function(){
    layer.photos({
        photos: '#goods-photos'
        ,shift: 5
    });
});

$(".goods_del").on('click',{btn:['确定','取消']},function(){
    var id= $(this).attr('data-id');
    layer.confirm('确定删除该商品?',function(){
        var index = layer.msg('删除中，请稍候',{icon: 16,time:false,shade:0.8});
        console.log(id);
        $.ajax({
            url:'/goods/delgoods',
            type:'get',
            data:{id:id},
            dataType:'json',
            timeout:2000,
            complete:function(){
                layer.close(index);
            },
            success:function(ret){
                if(ret.status==1){
                    history.go(0);
                    layer.msg(ret.msg);
                }else{
                    layer.alert(ret.msg, {icon: 2}, function(index){
                        layer.close(index);
                    });
                }
            },
            error:function(){
                layer.alert('参数错误', {icon: 2}, function(index){
                    layer.close(index);
                });
            }
        })

    },function(){
        layer.msg('你已取消该操作',{time:2000});
    });
});



